package test;

import java.sql.*;

public class queryFilght {
    public static void main(String[] args) throws SQLException {
        //1. 注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/airlines?useSSL=false";
        String username = "root";
        String password = "1234";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3. 定义sql
        String sql="select *from air_flight;";
        //4. 获取执行sql的对象 Statement
        Statement stmt = conn.createStatement();
        //5. 执行sql
        ResultSet rs = stmt.executeQuery(sql);//查询结果为：集合，则用ResultSet来获取结果
        //6. 处理结果 光标向下移动一行，并且判断当前行是否有数据，获取数据： getXxx()
        while (rs.next()){
            int flight_id=rs.getInt("flight_id");//后面的id指的是数据库里名为id的属性
            int flight_num=rs.getInt("flight_num");
            int airway_id=rs.getInt("airway_id");
            Date departure_time= Date.valueOf(rs.getString("departure_time"));
            Date arrival_time= Date.valueOf(rs.getString("arrival_time"));

//            Date time=arrival_time-departure_time;

            int aircraft_id=rs.getInt("aircraft_id");
            int price=rs.getInt("price");
            int available_seat=rs.getInt("available_seat");

            System.out.println("flight_id = " + flight_id);
            System.out.println("flight_num = " + flight_num);
            System.out.println("airway_id = " + airway_id);
            System.out.println("departure_time = " + departure_time);
            System.out.println("arrival_time = " + arrival_time);
            System.out.println("aircraft_id = " + aircraft_id);
            System.out.println("price = " + price);
            System.out.println("available_seat = " + available_seat);
            System.out.println("------------------------------------");
        }
        //7. 释放资源
        stmt.close();
        conn.close();
    }
}

